package com.linghu.qqclient.view;

import com.linghu.qqclient.service.FileClientService;
import com.linghu.qqclient.service.MessageClientService;
import com.linghu.qqclient.service.UserClientService;
import com.linghu.qqcommon.Message;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 客户单菜单界面
 */
public class QQView {
    private boolean loop=true;//控制显示菜单
    private String key="";
    Scanner scanner=new Scanner(System.in);
    private UserClientService userClientService=new UserClientService();//这个对象用于登录服务器，注册用户
    private MessageClientService messageClientService=new MessageClientService();//该对象与用户消息私聊、群聊

    private FileClientService fileClientService=new FileClientService();
    public static void main(String[] args) throws IOException {
        QQView qV = new QQView();
        qV.mainMenu();
    }
    //显示主菜单
    private void mainMenu() throws IOException {
        while (loop){

            System.out.println("============欢迎登录网络通讯系统===========");
            System.out.println("\t\t1、登录系统");
            System.out.println("\t\t9、退出系统");
            System.out.println("请输入你的选择：");
            key=scanner.next();

            switch (key){
                case "1":
                    System.out.println("请输入用户号：");
                    String userId=scanner.next();
                    System.out.println("请输入密  码：");
                    String pwd=scanner.next();
                    //这里编写一个UserClientService实现 完成用户注册登录验证
                    if (userClientService.checkUser(userId,pwd)){//先写逻辑
                        System.out.println("========欢迎("+userId+"登录成功)===============");
                        //进入二级菜单
                        while (loop){
                            System.out.println("\n=======网络通讯二级菜单（用户"+userId+"）=============");
                            System.out.println("\t\t1 显示在线用户列表");
                            System.out.println("\t\t2 群发消息");
                            System.out.println("\t\t3 私聊消息");
                            System.out.println("\t\t4 发送文件");
                            System.out.println("\t\t9 退出系统");
                            System.out.println("请输入你的选择：");
                            key=scanner.next();
                            switch (key){
                                case "1":
                                    userClientService.onlineFriends();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的话：");
                                    String s=scanner.next();
                                    //调用一个方法，将消息封装成message对象，发送给服务端
                                    messageClientService.sendMessageToAll(s,userId);


                                    break;
                                case "3":
                                    System.out.println("请输入想聊天的用户号(在线)：");
                                    String getterId=scanner.next();
                                    System.out.print("请输入想说的话：");
                                    String content=scanner.next();
                                    //编写一个方法，将消息发送给服务端
                                    messageClientService.sendMessageToOne(content,userId,getterId);
                                    break;
                                case "4":
                                    System.out.println("请输入你想要发送文件给的用户(在线用户)");
                                    String getterid=scanner.next();
                                    System.out.println("请输入你要发送的文件路径(形如：d:\\xx.jpg)");
                                    String src=scanner.next();
                                    System.out.println("请输入发送文件到对方的路径：");
                                    String dest=scanner.next();
                                    fileClientService.senFileToOne(src,dest,userId,getterid);
                                    break;
                                case "9":
                                    userClientService.logout();
                                    loop=false;
                                    break;
                            }
                        }
                    }else {
                        System.out.println("登录失败!");
                    }
                    break;
                case "9":
                    System.out.println("退出系统");
                    loop=false;
                    break;
            }
        }
        System.out.println("客户端退出系统。。。");
    }
}
